home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_show / gcd / integer.e < prev    next >
Text File  |  1997-04-13  |  5KB  |  223 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. expanded class  INTEGER
  5.  
  6. inherit
  7.    INTEGER_REF
  8.       redefine
  9.      infix "+", infix "-", infix "*", infix "/",
  10.      infix "\\", infix "//", infix "<",
  11.      compare, prefix "-", prefix "+"
  12.       end;
  13.  
  14. feature {ANY}
  15.    
  16.    infix "+"(other: INTEGER): INTEGER is
  17.      -- Add `other' to Current.
  18.       external "CSE"
  19.       end;
  20.    
  21.    infix "-" (other : INTEGER): INTEGER is
  22.      -- Subtract `other' from Current.
  23.       external "CSE"
  24.       end;
  25.  
  26.    infix "*" (other : INTEGER) : INTEGER is
  27.      -- Multiply `other' by Current.
  28.       external "CSE"
  29.       end;
  30.  
  31.    infix "/" (other : INTEGER): INTEGER is
  32.      -- Divide Current by `other'.
  33.      -- Note : Integer division
  34.       external "CSE"
  35.       end;
  36.  
  37.    infix "//" (other : INTEGER) : INTEGER is
  38.      -- Divide Current by `other'.
  39.      -- Note : Integer division
  40.       external "CSE"
  41.       end;
  42.  
  43.    infix "\\" (other : INTEGER) : INTEGER is
  44.      -- Remainder of division of Current by `other'.
  45.       external "CSE"
  46.       end;
  47.    
  48.    infix "<" (other: INTEGER): BOOLEAN is
  49.      -- Is Current less than `other'?
  50.       external "CSE"
  51.       end;
  52.  
  53.    prefix "+": INTEGER is
  54.       do
  55.      Result := Current
  56.       end;
  57.  
  58.    prefix "-" : INTEGER is
  59.      -- Unary minus of Current
  60.       external "CSE"
  61.       end;
  62.    
  63.    compare(other: INTEGER): INTEGER is
  64.      -- Compare Current with `other'.
  65.      -- '<' <==> Result < 0
  66.      -- '>' <==> Result > 0
  67.      -- Otherwise Result = 0
  68.       do
  69.      Result := Current - other
  70.       end;
  71.  
  72.    to_string: STRING is
  73.      -- Convert the INTEGER into a new allocated STRING. 
  74.      -- Note: see `append_in' to save memory.
  75.       do
  76.      !!Result.make(0);
  77.      append_in(Result);
  78.       end; 
  79.  
  80.    append_in(str: STRING) is
  81.      -- Append the equivalent of `to_string' at the end of 
  82.      -- `str'. Thus you can save memory because no other
  83.      -- STRING is allocate for the job.
  84.       require
  85.      str /= Void;
  86.       local
  87.      val, i: INTEGER;
  88.       do
  89.      if Current = 0 then
  90.         str.extend('0');
  91.      else
  92.         if Current < 0 then
  93.            str.extend('-');
  94.            (- Current).append_in(str);
  95.         else
  96.            from
  97.           i := str.count + 1;
  98.           val := Current;
  99.            until
  100.           val = 0
  101.            loop
  102.           str.extend((val \\ 10).digit);
  103.           val := val // 10;
  104.            end;
  105.            from  
  106.           val := str.count;
  107.            until
  108.           i >= val
  109.            loop
  110.           str.swap(i,val);
  111.           val := val - 1;
  112.           i := i + 1;
  113.            end;        
  114.         end;
  115.      end;
  116.       end; 
  117.    
  118.    to_string_format(s: INTEGER): STRING is
  119.      -- Same as `to_string' but the result is on `s' character and the 
  120.      -- number is right aligned. 
  121.      -- Note: see `append_in_format' to save memory.
  122.       require
  123.      to_string.count <= s;
  124.       do
  125.      from  
  126.         tmp_string.clear;
  127.         append_in(tmp_string);
  128.      until
  129.         tmp_string.count >= s
  130.      loop
  131.         tmp_string.add_first(' ');
  132.      end;
  133.      Result := clone(tmp_string);
  134.       ensure
  135.      Result.count = s;
  136.       end; 
  137.  
  138.    append_in_format(str: STRING; s: INTEGER) is
  139.      -- Append the equivalent of `to_string_format' at the end of 
  140.      -- `str'. Thus you can save memory because no other
  141.      -- STRING is allocate for the job.
  142.       do
  143.      from
  144.         tmp_string.clear;
  145.         append_in(tmp_string);
  146.      until
  147.         tmp_string.count >= s
  148.      loop
  149.         tmp_string.add_first(' ');
  150.      end;
  151.      str.append(tmp_string);
  152.       ensure
  153.      str.count >= (old str.count) + s;
  154.       end;
  155.    
  156.    digit: CHARACTER is
  157.      -- Gives the corresponding CHARACTER for range 0..9.
  158.       require
  159.      0 <= Current;
  160.      Current <= 9;
  161.       do
  162.      Result := ("0123456789").item(Current + 1);
  163.       ensure
  164.      ("0123456789").has(Result);
  165.      Result.value = Current;
  166.       end;
  167.       
  168.    gcd(other: INTEGER): INTEGER is
  169.      -- Great Common Divisor of `Current' and `other'.
  170.       require
  171.      Current > 0;
  172.      other > 0;
  173.       local
  174.      the_other: INTEGER;
  175.       do
  176.      from  
  177.         Result := Current;
  178.         the_other := other;
  179.      invariant
  180.         Result > 0;
  181.         the_other > 0;
  182.         Result.gcd(the_other) = Current.gcd(other);
  183.      variant
  184.         Result.max(the_other)
  185.      until
  186.         Result = the_other
  187.      loop
  188.         if Result > the_other then
  189.            Result := Result - the_other;
  190.         else
  191.            the_other := the_other - Result;
  192.         end;
  193.      end;
  194.       ensure
  195.      Result = other.gcd(Current);
  196.       end;
  197.    
  198. feature {NONE}
  199.    
  200.    tmp_string: STRING is "0123456789";
  201.      
  202.    to_character_table : STRING is "%
  203.      %%/000/%/001/%/002/%/003/%/004/%/005/%/006/%/007/%
  204.      %%/008/%/009/%/010/%/011/%/012/%/013/%/014/%/015/%
  205.      %%/016/%/017/%/018/%/019/%/020/%/021/%/022/%/023/%
  206.      %%/024/%/025/%/026/%/027/%/028/%/029/%/030/%/031/%
  207.      %%/032/%/033/%/034/%/035/%/036/%/037/%/038/%/039/%
  208.      %%/040/%/041/%/042/%/043/%/044/%/045/%/046/%/047/%
  209.      %%/048/%/049/%/050/%/051/%/052/%/053/%/054/%/055/%
  210.      %%/056/%/057/%/058/%/059/%/060/%/061/%/062/%/063/%
  211.      %%/064/%/065/%/066/%/067/%/068/%/069/%/070/%/071/%
  212.      %%/072/%/073/%/074/%/075/%/076/%/077/%/078/%/079/%
  213.      %%/080/%/081/%/082/%/083/%/084/%/085/%/086/%/087/%
  214.      %%/088/%/089/%/090/%/091/%/092/%/093/%/094/%/095/%
  215.      %%/096/%/097/%/098/%/099/%/100/%/101/%/102/%/103/%
  216.      %%/104/%/105/%/106/%/107/%/108/%/109/%/110/%/111/%
  217.      %%/112/%/113/%/114/%/115/%/116/%/117/%/118/%/119/%
  218.      %%/120/%/121/%/122/%/123/%/124/%/125/%/126/%/127/%
  219.      %";
  220.      
  221. end -- class INTEGER
  222.  
  223.